{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c8af4bd6",
   "metadata": {},
   "source": [
    "--------\n",
    "* 记录人：郭俊彬\n",
    "* 时间：2021/12/09\n",
    "* week12\n",
    "* 主要参与文档：\n",
    "> [python官方文档](https://python.org/doc)    \n",
    "> [gitee课堂笔记](https://gitee.com/xzhichao/python)  \n",
    "> [老师的博客](https://www.cnblogs.com/xuzhichao/p/11570285.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fa845aa",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "> 1.函数的定义 ： def  \n",
    "> 2.函数名的定义规则-->变量名一样的规则  \n",
    "> 3.调用函数的方式  \n",
    "> 4.参数:  \n",
    ">> 1.关键字参数  \n",
    ">> 2.位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）  \n",
    ">> 3.默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57bb0594",
   "metadata": {},
   "source": [
    "|序号|内容|\n",
    "|---|--------|\n",
    "|1|函数的定义 ： def|\n",
    "|2|.函数名的定义规则-->变量名一样的规则|\n",
    "|3|调用函数的方式|\n",
    "|4|参数:|\n",
    "|4.1|关键字参数|\n",
    "|4.2|位置参数（调用函数时）---> 关键字参数要均放在末尾（多个不考虑顺序）|\n",
    "|4.3|默认参数（定义函数时）---> 默认参数均放在末尾（多个不考虑顺序）|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "985bd9bb",
   "metadata": {},
   "source": [
    "# 本周知识\n",
    "> 1.函数的返回值  \n",
    "> 2.函数的参数类型  \n",
    "> 3.模块与函数的关系  \n",
    "> 4.定义一个模块，然后调用模块  \n",
    ">> 4.1调用模块  \n",
    ">> 4.2调用模块  \n",
    ">\n",
    "> 5.模块与函数实践  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9287e13f",
   "metadata": {},
   "source": [
    "## 函数的返回值\n",
    "> 1.函数一定有返回值（无论是什么编程语言）  \n",
    "> 2.在其他的编程语言中（如c语言，java等），返回值一定要写，不写会报错！；对于python（更高级的语言），返回值如果不写，给定默认值 None  \n",
    "> 3.返回值一般为函数体运行的结果（result）（函数返回值实践1）;也可以是自定义的函数返回值(实践2)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "630ea9a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数返回值实践1\n",
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "#     print(full_name)\n",
    "    return full_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cddf22ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Li Hua\n"
     ]
    }
   ],
   "source": [
    "print(get_formatted_name(\"Li\",\"Hua\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a9fb1aab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践2\n",
    "def log_in(username,password):\n",
    "    \"\"\"验证登录信息\"\"\"\n",
    "    if username == \"Lihua\" and password == 123456:\n",
    "        return \"认证成功\"\n",
    "    else:\n",
    "        return \"认证失败\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b7a279c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'认证失败'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "log_in(\"Lihua\",12345)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a72caa7",
   "metadata": {},
   "source": [
    "## 函数的参数\n",
    "\n",
    "### 函数的输入参数类型\n",
    "\n",
    "* 1.函数可以输入复杂的参数:\n",
    "> 函数参数为列表\n",
    "> 函数参数为字典\n",
    "* 函数可以输入不确定个数的参数：\n",
    "> args  \n",
    "> kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a5105bd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def list_test(names):\n",
    "    \"\"\"测试输入参数为列表\"\"\"\n",
    "    print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d5b4e6b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Lihua', 'Xiaofeng', 'Ziqi']\n"
     ]
    }
   ],
   "source": [
    "list_test([\"Lihua\",\"Xiaofeng\",\"Ziqi\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1cc97070",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_B(person):\n",
    "    \"\"\"测试函数参数输入为字典\"\"\"\n",
    "    return person"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "85ddc162",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_B({\"stud_id\":210001,\"age\":18,\"Job\":\"student\"})"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1031cc98",
   "metadata": {},
   "source": [
    "## 函数的输出（返回值）参数类型\n",
    "> 在编程语言中，函数的返回值只能是一个值  \n",
    "> 返回值可以是 字符串、数值、列表、字典等数据（单一值）  \n",
    "> python可以输出多个返回值，结果会用一个元组进行封装并返回。（本质不违背函数的定义）  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6894b97d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test1_return():\n",
    "    \"\"\"测试返回值return为字符串\"\"\"\n",
    "    return \"你好\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "72d5173e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test1_return()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "521abc6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test2_return():\n",
    "    \"\"\"测试返回值return为数值\"\"\"\n",
    "    return 12345"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2d2acb89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12345"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test2_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d2e57472",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test3_return():\n",
    "    \"\"\"测试返回值return为列表\"\"\"\n",
    "    return ['Lihua', 'Alex', 'Ziqi']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "9020bca0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Lihua', 'Alex', 'Ziqi']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test3_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5b2f961f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test4_return():\n",
    "    \"\"\"测试返回值return为字典\"\"\"\n",
    "    return {'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "1b44fb1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test4_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1c11b501",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ******** return 后面写多个值，将会返回一个元组（一个值，不违背函数在编程思维中的定义）\n",
    "def test5_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 18,\"Lihua\",['Lihua', 'Alex', 'Ziqi'],{'stud_id': 210001, 'age': 18, 'Job': 'student'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b0fd6fdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(18,\n",
       " 'Lihua',\n",
       " ['Lihua', 'Alex', 'Ziqi'],\n",
       " {'stud_id': 210001, 'age': 18, 'Job': 'student'})"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test5_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "d7d1a9fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def test6_return():\n",
    "    \"\"\"测试返回值return 为 多个值的情况\"\"\"\n",
    "    return 88,89,90,90"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "3ad1595f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(88, 89, 90, 90)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test6_return()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b43c2d9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cf2748cd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ea93ab23",
   "metadata": {},
   "outputs": [],
   "source": [
    "import study"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "50a5a8f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "study.search4vowels(\"hello world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "c716919b",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'study' has no attribute 'news_链接'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-35-7c00a969a59c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstudy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnews_链接\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'study' has no attribute 'news_链接'"
     ]
    }
   ],
   "source": [
    "help(study.news_链接)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
